/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import POJO.NhanVien;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
public class NhanVienDAO {
    
    /**
     * @desc lay danh sach nhan vien
     * @return 
     */
    public static List<NhanVien> layDanhSach() {
        List<NhanVien> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from NhanVien nv";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim nhan vien theo ma nhan vien
     * @param maNhanVien
     * @return 
     */
    public static NhanVien layThongTinNhanVien(int maNhanVien) {
        NhanVien nv = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            nv = (NhanVien) session.get(NhanVien.class, maNhanVien);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return nv;
    }
    
    /**
     * @desc them nhan vien
     * @param nv
     * @return 
     */
    public static boolean themNhanVien(NhanVien nv) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NhanVienDAO.layThongTinNhanVien(nv.getMaNv())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin nhan vien
     * @param nv
     * @return 
     */
    public static boolean capNhatThongTinNhanVien(NhanVien nv) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (NhanVienDAO.layThongTinNhanVien(nv.getMaNv()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa nhan vien
     * @param maNhanVien
     * @return 
     */
    public static boolean xoaNhanVien(int maNhanVien) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        NhanVien nv = NhanVienDAO.layThongTinNhanVien(maNhanVien); 
        if(nv==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(nv); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
    
}
